<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="nickName"
        name="nickName"
        label="昵称"
        placeholder="昵称"
        :rules="[
          { required: true, message: '请填写昵称' },
          {
            pattern: /^[a-z_$][0-9a-z_$]{3,9}$/i,
            message: '只能是数字字母_$组成不能是数字开头长度4到10位',
          },
        ]"
      />
      <van-field name="avatar" label="头像">
        <template #input>
          <van-uploader v-model="uploader" />
        </template>
      </van-field>

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import mixin from "../mixin/flag";
import { editUser } from "../service/mineService";
import { upload } from "../service/common";
export default {
  mixins: [mixin],
  data() {
    return {
      nickName: "", //昵称
      avatar: "", //头像
      uploader: [],
    };
  },
  methods: {
    async onSubmit(values) {
      console.log(values);
      let formData = new FormData();
      formData.append("file", values.avatar[0]["file"]);

      let res = await upload(formData);

      if (res.data.code == 200) {
        // console.log("http://124.93.196.45:10001/prod-api/" + res.data.fileName);
        let res2 = await editUser({
          nickName: values.nickName,
          avatar:  res.data.fileName,
        });
        if (res2.data.code == 200) {
          this.$router.go(-1);
        }
      }
    },
  },
};
</script>

<style></style>
